package cn.wps.sdk.demo.aidl;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

import cn.wps.moffice.client.ActionType;
import cn.wps.moffice.client.OfficeInputStream;
import cn.wps.moffice.client.OfficeOutputStream;
import cn.wps.moffice.client.ViewType;
import cn.wps.moffice.sdk.WpsSdk;
import cn.wps.moffice.sdk.api.moffice.OfficeApi;
import cn.wps.moffice.sdk.api.moffice.OfficeEventListener;
import cn.wps.moffice.sdk.api.moffice.utils.FileUtil;
import cn.wps.moffice.sdk.api.util.OpenParams;
import cn.wps.moffice.service.OfficeService;
import cn.wps.moffice.service.doc.Document;
import cn.wps.sdk.demo.base.FileItem;
import cn.wps.sdk.demo.base.SingleSelectFileActivity;

public class AIDLActivity extends SingleSelectFileActivity {

    @Override
    public String executeName() {
        return "打开文档";
    }

    @Override
    public void execute(List<FileItem> files) {
        if (files.size() == 0) {
            Toast.makeText(AIDLActivity.this, "请先选择文件", Toast.LENGTH_SHORT).show();
            return;
        }
        //String filePath = "/sdcard/欢迎使用WPS Office.docx";
//      String filePath = "/storage/emulated/0/Android/data/cn.wps.sdk.demo/files/document/123.docx";
        openFileByAidl(files.get(0).filePath);
    }

    Document mDoc;

    private void openFileByAidl(String filePath) {
        OfficeApi officeApi = WpsSdk.getInstance().getApi(OfficeApi.class);
        if (officeApi != null) {
            Bundle bundle = new Bundle();
            bundle.putString(OpenParams.OpenMode, "EditMode");
            bundle.putBoolean(OpenParams.ClearTrace, true);
            bundle.putInt("ScreenOrientation", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            new Thread(() -> {
                try {
                    OfficeService service;
                    //以不落地的方式打开文件，需要注册打开事件，自行处理打开保存逻辑
                    officeApi.addOfficeEventListener(new OfficeEventListener() {
                        @Override
                        public int onOpenFile(String tmpPath, OfficeOutputStream output) throws RemoteException {
                            Log.d("WpsSdkLog", "demo addEventListener onOpenFile: tmpPath=" + tmpPath);
                            //TODO 客户自己处理打开逻辑，客户可以读取自己私有路径下的文件流或者服务端文件流，然后将文件流传递给WPS
                            //文档打开前回调，将文件流传递给WPS， 这里客户根据自己的逻辑自行处理
                            int ret = FileUtil.normalOpenFile(tmpPath, output);
                            return ret;
                        }

                        //
                        @Override
                        public int onSaveFile(OfficeInputStream input, String tmpPath) throws RemoteException {
                            Log.d("WpsSdkLog", "demo addEventListener onSaveFile: tmpPath=" + tmpPath);
                            //保存文件回调，WPS将文件流传递给第三方，进行保存处理
                            //TODO 客户自行接受WPS传递的文件输出流，将文件写入自己想要保存的文件路径，比如上传给OA服务端
                            int ret = FileUtil.normalSaveFile(input, tmpPath);
                            return ret;
                        }


                        @Override
                        public boolean onActionForbidden(String path, ActionType type) throws RemoteException {
                            if (type.equals(ActionType.AT_COPY)) {
                                Log.d("WpsSdkLog", "demo addEventListener onActionForbidden: path=" + path + " type=" + type);
                                return true;
                            }
                            return OfficeEventListener.super.onActionForbidden(path, type);
                        }

                        @Override
                        public boolean onViewForbidden(String path, ViewType type) throws RemoteException {
                            //将另存按钮-禁用
                            if (type.equals(ViewType.VT_FILE_SAVEAS) || type.equals(ViewType.VT_FILE_SAVE)) {
                                Log.d("WpsSdkLog", "demo addEventListener onViewForbidden: path=" + path + " type=" + type);
                                return true;
                            }
                            return OfficeEventListener.super.onViewForbidden(path, type);
                        }

                    });

                    service = officeApi.openFile(AIDLActivity.this, filePath, bundle);
                    Log.d("WpsSdkLog", "demo openFileByAidl: service=" + service);
                    mDoc = service.getActiveDocument();
                    mDoc.getSelection();
                    Log.d("WpsSdkLog", "demo openFileByAidl: doc=" + mDoc);
                    Log.d("WpsSdkLog", "demo openFileByAidl: doc.getName()=" + mDoc.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();

        }

    }
}


